package com.wing.product.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseProductionOrder;
import com.wing.product.model.response.WarehouseProductionOrderVO;
import com.wing.product.service.WarehouseProductionOrderService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "生产订单管理", tags = "#/warehouse/product/warehouseProductionOrder")
@RestController("WarehouseProductionOrderController")
@RequestMapping("/warehouse/productionOrder")
@RequiredArgsConstructor
public class WarehouseProductionOrderController {

    private final WarehouseProductionOrderService warehouseProductionOrderService;

    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseProductionOrderVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "99", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            String customerName,
            String productionOrderNumber,
            String status,
            Long customerId,
            String userBundleCode
    ){
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseProductionOrder> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("customerName", customerName);
        paramMap.put("userBundleCode", userBundleCode);
        paramMap.put("productionOrderNumber", productionOrderNumber);
        paramMap.put("status", status);
        paramMap.put("customerId", customerId);

        //搜索
        IPage<WarehouseProductionOrderVO> list = warehouseProductionOrderService.pageList4app(pg,paramMap);

        //搜集加工产品数
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "查询生产订单明细", notes = "查询")
    @GetMapping ("/selectProductionDetail")
    public JsonResult<WarehouseProductionOrderVO> selectProductionDetail(Integer state ,@RequestParam String id){
        if (!StringUtils.isEmpty(id)){
            WarehouseProductionOrderVO warehouseProductionOrderVO = warehouseProductionOrderService.selectProductionDetail(state,id);
            if (ObjectUtil.isNotEmpty(warehouseProductionOrderVO)){
                return  JsonResult.success("请求成功",warehouseProductionOrderVO);
            }else {
                return  JsonResult.success("数据为空",warehouseProductionOrderVO);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "添加生产订单", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseProductionOrderVO warehouseProductionOrderVO)  {
        try{
            return warehouseProductionOrderService.saveWarehouseProductionOrder(warehouseProductionOrderVO);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "修改生产订单", notes = "修改")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/updateProductionOrder")
    public JsonResult updateProductionOrder(@RequestBody WarehouseProductionOrderVO warehouseProductionOrderVO)  {
        try{
            return warehouseProductionOrderService.updateProductionOrder(warehouseProductionOrderVO);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "修改生产订单状态", notes = "修改")
    @GetMapping ("/updateProduction")
    public JsonResult updateProduction(@RequestParam String id,@RequestParam String status){
        if (!StringUtils.isEmpty(id)){
                return  warehouseProductionOrderService.updateProduction(id,status);
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "看板订单趋势", notes = "搜索")
    @GetMapping("/selectOrderTrend")
    public JsonResult<Map<Object, Object>> selectOrderTrend() {
        return warehouseProductionOrderService.selectOrderTrend();
    }

    @ApiOperation(value = "生产看板订单", notes = "搜索")
    @GetMapping("/selectProductionOrderTrend")
    public JsonResult<Map<Object, Object>> selectProductionOrderTrend() {
        return warehouseProductionOrderService.selectProductionOrderTrend();
    }
}
